﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Migrator.Entities;
using Migrator.Util;
using Migrator.Packets;

namespace Migrator.Services.Advertisement.Packets
{
    /// <summary>
    /// MobileIP advertisement packet.
    /// 
    /// Structure:
    /// 
    /// 0                   1                   2                   3
    /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    /// |     Type      |    Length     |        Sequence Number        |
    /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    /// |    Registration Lifetime      |R|B|H|F|M|G|r|T|   reserved    |
    /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    /// |                  zero or more Care-of Addresses               |
    /// |                              ...                              |
    /// </summary>
    public class MobileIPAdvertisementPacket : ICMPRouterAdvertisementPacket
    {
        public MobileIPAdvertisementPacket(Node source)
            : base(source)
        {
            this.CareOfAddresses = new List<IPAddress>();
        }


        #region Packet Flags

        public int ExtensionType
        {
            get { return 16; }
        }

        /// <summary>
        /// The longest lifetime (measured in seconds) that this
        /// agent is willing to accept in any Registration Request.
        /// A value of 0xffff indicates infinity.  This field has no
        /// relation to the "Lifetime" field within the ICMP Router
        /// Advertisement portion of the Agent Advertisement.
        /// </summary>
        public ushort RegistrationLifeTime
        {
            get;
            set;
        }
        
        /// <summary>
        /// Registration required.
        /// Registration with this foreign agent (or another foreign
        /// agent on this link) is required even when using a 
        /// co-located care-of address.
        /// </summary>
        public bool R
        {
            get;
            set;
        }

        /// <summary>
        /// Busy. 
        /// The foreign agent will not accept registrations
        /// from additional mobile nodes.
        /// </summary>
        public bool B
        {
            get;
            set;
        }

        /// <summary>
        /// Home agent
        /// This agent offers service as a home agent on the link
        /// on which this Agent Advertisement message is sent.
        /// </summary>
        public bool H
        {
            get;
            set;
        }

        /// <summary>  
        /// Foreign agent.
        /// This agent offers service as a foreign agent on the 
        /// link on which this Agent Advertisement
        /// message is sent.
        /// </summary>
        public bool F
        {
            get;
            set;
        }
        
        /// <summary>
        /// Minimal encapsulation.
        /// This agent implements receiving tunneled datagrams that 
        /// use minimal encapsulation.
        /// </summary>
        public bool M
        {
            get;
            set;
        }

        /// <summary>
        /// GRE encapsulation. This agent implements receiving
        /// tunneled datagrams that use GRE encapsulation.
        /// </summary>
        public bool GRE
        {
            get;
            set;
        }

        /// <summary>
        /// Foreign agent supports reverse tunneling.
        /// </summary>
        public bool T
        {
            get;
            set;
        }

        /// <summary>
        /// The advertised foreign agent care-of address(es) provided
        /// by this foreign agent. An Agent Advertisement MUST
        /// include at least one care-of address if the 'F' bit is
        /// set. The number of care-of addresses present is
        /// determined by the Length field in the Extension.
        /// </summary>
        public List<IPAddress> CareOfAddresses
        {
            get;
            set;
        }

        /// <summary>
        /// Packet sequence number
        /// </summary>
        public int SequenceNumber
        {
            get;
            set;
        }        

        #endregion       
    }
}
